package com.pisolution.scheduler.runtime.quartz;

import com.pisolution.scheduler.runtime.SchedulerContext;

import com.alibaba.druid.pool.DruidDataSource;
import org.quartz.utils.ConnectionProvider;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * 用于提供数据库访问接口 适用于定时调度服务存储任务数据.
 *
 * @author sora
 * @version 0.1.0
 */
public class DruidConnectionProvider implements ConnectionProvider {
    /** 用于访问数据源. */
    private final DruidDataSource dataSource;

    // -------------------------------------------------------------------------------------------------
    //  初始化
    // -------------------------------------------------------------------------------------------------

    public DruidConnectionProvider() {
        this.dataSource = SchedulerContext.get(DruidDataSource.class);
    }

    // -------------------------------------------------------------------------------------------------
    //  数据源
    // -------------------------------------------------------------------------------------------------

    @Override
    public Connection getConnection() throws SQLException {
        return this.dataSource.getConnection();
    }

    // -------------------------------------------------------------------------------------------------
    //  生命周期
    // -------------------------------------------------------------------------------------------------

    @Override
    public void initialize() {
        // 无需处理.
    }

    @Override
    public void shutdown() {
        this.dataSource.close();
    }
}
